package com.chookss.video

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chookss.R
import com.chookss.mine.personal.PersonalHomeActivity
import com.chookss.tools.GlideUtils
import com.chookss.tools.RecyclerClickAdapter
import com.chookss.tools.Utils
import com.chookss.video.entity.VideoEntity
import com.chookss.video.model.VideoModel
import com.chookss.view.RecycleViewDivider
import com.johnrambo.ktea.ktExtends.observe
import errorToast
import kotlinx.android.synthetic.main.fragment_video_recommends.*
import kotlinx.android.synthetic.main.item_video_recommends_3.view.*
import kotlinx.android.synthetic.main.layout_common_empty.*

/**
 * Company:
 * Project:
 * Author: liuhaoge
 * Date: 2021/1/26 18:48
 * Note:
 */
class VideoRecommendsFragment3 : Fragment() {
    fun VideoRecommendsFragment3() {}
    private var emptyParams: LinearLayout.LayoutParams? = null
    lateinit var adapter: MyAdapter
    private val videoModel by lazy {
        ViewModelProviders.of(this).get(VideoModel::class.java)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_video_recommends, null)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        adapter = MyAdapter()
        recyclerView.adapter = adapter
        tvEmpty.text="暂无明星数据"
        ivEmpty.setBackgroundResource(R.drawable.icon_default_no_search)
        emptyParams = ivEmpty?.getLayoutParams() as LinearLayout.LayoutParams?
        emptyParams?.setMargins(0, Utils.dip2px(context, 40f), 0, 0)
        ivEmpty?.setLayoutParams(emptyParams)
        recyclerView.addItemDecoration(
            RecycleViewDivider(
                context,
                LinearLayoutManager.HORIZONTAL,
                Utils.dip2px(context, 0.5f),
                getResources().getColor(R.color.line_gray)
            )
        )
        recyclerView.layoutManager =
            LinearLayoutManager(view.context, LinearLayoutManager.VERTICAL, false)
        videoModel.selectRecommend("3").observe(viewLifecycleOwner, {
            if (it.error != null) {
                activity?.errorToast(it.error.message ?: "")
            } else {
                adapter.datas = it.data
                adapter.notifyDataSetChanged()
                if(it.data.size==0){
                    recyclerView.visibility=View.GONE
                    llEmpty.visibility=View.VISIBLE
                }
            }
        })
        adapter.setOnItemClickListener { adapter, data, holder ->
            startActivity(Intent(context, VideoOnePlayActivity::class.java).apply {
                putExtra("video", data)
                putExtra("watermark", "2")

            })
        }
    }

    class MyAdapter : RecyclerClickAdapter<VideoEntity, RecyclerView.ViewHolder>() {
        var datas: List<VideoEntity>? = null
        override fun getItemCount() = datas?.size ?: 0

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            return object : RecyclerView.ViewHolder(
                LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_video_recommends_3, parent, false)
            ) {}
        }

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            var view = holder.itemView
            var item = datas!![position]
            GlideUtils.loadAvatar(
                view.context,
                item.headPhotoThumbUrl,
                view.ivAvatar
            )
            view.tvIndex.text = "${position + 1}、"
            view.tvCount.text = item.videoPlayCounts.toString() + "次"
            view.tvName.text = item.employeeName
            view.ivAvatar.setOnClickListener {
                val bundle = Bundle()
                bundle.putString("employeeCode", item.employeeCode)
                val intent = Intent(view.context, PersonalHomeActivity::class.java)
                intent.putExtras(bundle)
                view.context.startActivity(intent)
            }
        }

        override fun getItem(position: Int) = datas!![position]
    }

}